<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\MongoDBModels\Photos;
use Illuminate\Support\Facades\Log;
use MongoDB\BSON\Regex;

class PhotosController extends Controller
{

    /**
     * @param Request $request
     * @return false|string
     * @desc 写入照片数据
     */
    public function create(Request $request)
    {
        $params = $request->all();
        if (isset($params['photo_id']) && isset($params['words_result_num']) && isset($params['game_id'])) {
            $photo = Photos::where('game_id', (int)$params['game_id'])->where('photo_id', (int)$params['photo_id'])->first();
            if (empty($photo)) {
                $params['created_time'] = date("Y-m-d H:i:s", time());
                $params['updated_time'] = date("Y-m-d H:i:s", time());
                Photos::insert($params); //写入数据
            }
        }
        return $this->returnResponse(0, 'success', new \stdClass());
    }

    /**
     * @return void
     * @desc 获取照片id数据
     */
    public function list(Request $request)
    {
        $params = $request->all();
        if (!isset($params['words']) || empty($params['game_id'])) {
            return $this->returnResponse(400, '参数不能为空', []);
        }
        $photoList = Photos::query()->select('photo_id')->where('words_result.words', 'like', "%{$params['words']}%")->where('game_id', (int)$params['game_id'])->orderBy('photo_id', 'asc')->limit(500)->get();
        if (count($photoList) > 0) {
            $photoIds = array_column($photoList->toArray(), 'photo_id');
            return $this->returnResponse(0, 'success', $photoIds);
        }
        return $this->returnResponse(0, 'success', []);
    }


    /**
     * @param Request $request
     * @return void
     * @desc 多参赛号模糊查询
     */
    public function listNo(Request $request)
    {
        $params = $request->all();
        if (!isset($params['no']) || empty($params['game_id'])) {
            return $this->returnResponse(400, '参数不能为空', []);
        }
        $keywords = $params['no'];
        // 构造模糊查询条件
        $orConditions = [];
        foreach ($keywords as $keyword) {
            // 使用正则表达式进行模糊匹配，不区分大小写
            $pattern = new Regex(".*$keyword.*", 'i');
            $orConditions[] = [
                'words_result.words' => $pattern
            ];
        }
        // 设置查询条件
        $conditions = [
            'game_id' => (int)$params['game_id'],
            '$or' => $orConditions,
        ];
        // 执行查询
        $result = Photos::whereRaw($conditions)->get();
        if (count($result) > 0) {
            $photoIds = array_column($result->toArray(), 'photo_id');
            return $this->returnResponse(0, 'success', $photoIds);
        }
        return $this->returnResponse(0, 'success', []);
    }


    /**
     * @param Request $request
     * @return false|string
     * @desc 写入照片数据
     */
    public function createMany(Request $request)
    {
        $params = $request->all();
        // Log::info(json_encode($params));
        if (count($params) > 0) {
            foreach ($params as $key => $value) {
                $value['created_time'] = date("Y-m-d H:i:s", time());
                $value['updated_time'] = date("Y-m-d H:i:s", time());
                Photos::query()->insert($value); //写入数据
            }
        }

        return $this->returnResponse(0, 'success', []);
    }

    /**
     * @return void
     * @desc 关键字精确搜索
     */
    public function specificList(Request $request)
    {
        $params = $request->all();
        if (!isset($params['words']) || empty($params['game_id'])) {
            return $this->returnResponse(400, '参数不能为空', []);
        }
        $photoList = Photos::select('photo_id')->where('words_result.words', $params['words'])->where('game_id', (int)$params['game_id'])->where('words_result_num', '<', 5)->orderBy('photo_id', 'asc')->limit(500)->get();
        if (count($photoList) > 0) {
            $photoIds = array_column($photoList->toArray(), 'photo_id');
            return $this->returnResponse(0, 'success', $photoIds);
        }
        return $this->returnResponse(0, 'success', []);
    }

}
